package com.android.love.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.util.Constant;
import com.android.love.util.ImageUtils;
import com.android.love.vo.Order;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class MyOrderAdapter extends BaseAdapter implements Constant {

    private static final String TAG="MyOrderAdapter";

    private ImageLoader loader;

    private List<Order> orders=Collections.emptyList();;

    private Context context;

    private Handler handler;

    private int showNum;

    public MyOrderAdapter(Context context, ImageLoader loader, List<Order> orders, Handler handler) {
        super();
        this.context=context;
        this.loader=loader;
        this.orders=orders;
        this.handler=handler;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return orders.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // if (convertView == null) {
        convertView=View.inflate(context, R.layout.order_item, null);
        holder=new ViewHolder();
        holder.rl_cart_img=(RelativeLayout)convertView.findViewById(R.id.rl_cart_img);
        holder.ll_cart_img=(LinearLayout)convertView.findViewById(R.id.ll_cart_img);
        holder.iv_zaixianzhifu=(ImageView)convertView.findViewById(R.id.iv_zaixianzhifu);
        holder.iv_arrow=(ImageView)convertView.findViewById(R.id.iv_arrow);
        holder.tv_order_status=(TextView)convertView.findViewById(R.id.tv_order_status);
        holder.tv_cart=(TextView)convertView.findViewById(R.id.tv_cart);
        holder.tv_format=(TextView)convertView.findViewById(R.id.tv_format);
        holder.tv_price=(TextView)convertView.findViewById(R.id.tv_price);
        holder.tv_order_id=(TextView)convertView.findViewById(R.id.tv_order_id);
        holder.tv_order_time=(TextView)convertView.findViewById(R.id.tv_order_time);
        convertView.setTag(holder);
        // } else {
        // holder = (ViewHolder) convertView.getTag();
        // }
        // Goods good = new Goods();
        String html_status=null;
        final Order order=orders.get(position);
        switch(order.getStatus()) {
            case 1:
                html_status="<font color=\"#636363\">" + "订单状态： " + "</font>" + "<font color=\"#ffcd00\">" + "等待付款" + "</font>";
                // holder.tv_order_status.setText("订单状态：等待付款");
                holder.iv_zaixianzhifu.setImageResource(R.drawable.btn_zaixianzhifu);
                break;
            case 2:
                html_status="<font color=\"#636363\">" + "订单状态： " + "</font>" + "<font color=\"#ffcd00\">" + "等待发货" + "</font>";
                // holder.tv_order_status.setText("订单状态：等待发货");
                holder.iv_zaixianzhifu.setVisibility(View.GONE);
                break;
            case 3:
                html_status="<font color=\"#636363\">" + "订单状态： " + "</font>" + "<font color=\"#ffcd00\">" + "已发货" + "</font>";
                holder.iv_zaixianzhifu.setImageResource(R.drawable.btn_confirm_shouhuo);
                break;
            case 4:
                html_status="<font color=\"#636363\">" + "订单状态： " + "</font>" + "<font color=\"#ffcd00\">" + "已完成" + "</font>";
                // holder.tv_order_status.setText("订单状态：待确定收货");
                holder.iv_zaixianzhifu.setVisibility(View.GONE);
                break;
            case 5:
                html_status="<font color=\"#636363\">" + "订单状态： " + "</font>" + "<font color=\"#ffcd00\">" + "订单已取消" + "</font>";
                // holder.tv_order_status.setText("订单状态：已取消");
                holder.iv_zaixianzhifu.setVisibility(View.GONE);
                break;

            default:
                break;
        }
        holder.tv_order_status.setText(Html.fromHtml(html_status));
        if(order.getGoods() != null) {

            if(order.getGoods().size() == 1) {
                holder.tv_cart.setText(order.getGoods().get(0).getGoods_name());
                holder.tv_format.setText(order.getGoods().get(0).getAttribute());
            } else {
                holder.tv_cart.setVisibility(View.GONE);
                holder.tv_format.setVisibility(View.GONE);
            }
            if(order.getGoods().size() <= 3) {
                showNum=order.getGoods().size();
            } else {
                showNum=3;
            }
            for(int i=0; i < showNum; i++) {
                ImageView imgImageView=new ImageView(context);
                holder.ll_cart_img.addView(imgImageView);
                imgImageView.setBackgroundResource(R.drawable.beijing2);
                loader.displayImageForSize(order.getGoods().get(i).getGoods_image(), imgImageView,
                    ImageUtils.getOptions(R.drawable.zw3), new SimpleImageLoadingListener(), 15, 5, 15, 5, 0, 0, 6f, true);
            }
        }
        holder.tv_order_id.setText("订单编号：" + order.getOrder_id());
        holder.tv_order_time.setText("下单时间：" + order.getOrder_time());
        String html_price=
            "<font color=\"#636363\">" + "订单金额： " + "</font>" + "<font color=\"#e2017b\">" + "￥ " + order.getSum() + "</font>";
        holder.tv_price.setText(Html.fromHtml(html_price));
        holder.rl_cart_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 订单详情
                sendMessage(ORDER_LIST, position);
            }
        });
        holder.iv_zaixianzhifu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //
                sendMessage(ZHIFU, position);
            }
        });

        return convertView;
    }

    /**
     * 发消息
     * 
     * @param what
     * @param position
     */
    private void sendMessage(int what, int position) {
        Message msg=Message.obtain();
        msg.what=what;
        msg.arg1=position;
        handler.sendMessage(msg);
    }

    public static class ViewHolder {

        LinearLayout ll_cart_img;

        RelativeLayout rl_cart_img;

        ImageView iv_zaixianzhifu;

        ImageView iv_arrow;

        TextView tv_order_status;

        TextView tv_cart;

        TextView tv_format;

        TextView tv_price;

        TextView tv_order_id;

        TextView tv_order_time;
    }

}
